﻿using Model;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    /// <summary>
    /// 商家业务
    /// </summary>
    public class merchantDal
    {
        /// <summary>
        /// 通过商家ID查商家信息
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        public static ms_merchant Querybyid(string mid)
        {
            string sql = "select * from ms_merchant where id=@id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("id", mid);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            ms_merchant merchant = new ms_merchant();
            if (rd.Read())
            {
                merchant.id = rd["id"].ToString();
                merchant.merchant_name = rd["merchant_name"].ToString();
                merchant.merchant_tel = rd["merchant_tel"].ToString();
                merchant.logo = rd["logo"].ToString();
                merchant.cover = rd["cover"].ToString();
                merchant.business_hours = rd["business_hours"].ToString();               
                merchant.province = rd["province"].ToString();
                merchant.city = rd["city"].ToString();
                merchant.area = rd["area"].ToString();
                merchant.address = rd["address"].ToString();
                merchant.longitude = rd["longitude"].ToString();
                merchant.latitude = rd["latitude"].ToString();
                merchant.balance = rd["balance"].ToString();
                merchant.marketing_funds = rd["marketing_funds"].ToString();
                merchant.introduce = rd["introduce"].ToString();
                merchant.banks_user = rd["banks_user"].ToString();
                merchant.banks_name = rd["banks_name"].ToString();
                merchant.banks_card = rd["banks_card"].ToString();
                merchant.banks_address = rd["banks_address"].ToString();
                merchant.merchant_type = rd["merchant_type"].ToString();
                merchant.service_level = rd["service_level"].ToString();
                merchant.update_time = rd["update_time"].ToString();
                merchant.status = rd["status"].ToString();
                merchant.advertising_map = rd["advertising_map"].ToString();
                merchant.contact_person = rd["contact_person"].ToString();
                merchant.contact_person_tel = rd["contact_person_tel"].ToString();
                merchant.pay_status = rd["pay_status"].ToString();
                merchant.add_time = rd["add_time"].ToString();
                merchant.reason = rd["reason"].ToString();
                merchant.wechat_code = rd["wechat_code"].ToString();
                merchant.alipay_code = rd["alipay_code"].ToString();
                merchant.turnoff = rd["turnoff"].ToString();
                merchant.delivery_price = rd["delivery_price"].ToString();
                merchant.distribution_costs = rd["distribution_costs"].ToString();
                merchant.residential_quarters = rd["residential_quarters"].ToString();
               
            }
            rd.Close();
            return merchant;
        }
        /// <summary>
        /// 查餐位费
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        public static double Queryseatbyid(string mid)
        {
            string sql = "select seat_money from ms_merchant where id=@id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("id", mid);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            double seatmoney = 0;
            if (rd.Read())
            {
                seatmoney = Convert.ToDouble(rd["seat_money"]);
            }
            rd.Close();
            return seatmoney;
        }
        /// <summary>
        /// 修改banner
        /// </summary>
        /// <param name="banner"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Updatebanner(string banner, string merchantid)
        {
            string sql = "update ms_merchant set advertising_map=@advertising_map where id=@id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("advertising_map", banner);
            values[1] = new MySqlParameter("id", merchantid);
            return DBHelper.ExcutUpdate(sql, values);
        }

        /// <summary>
        /// 修改logo
        /// </summary>
        /// <param name="logo"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Updatelogo(string logo, string merchantid)
        {
            string sql = "update ms_merchant set logo=@logo where id=@id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("logo", logo);
            values[1] = new MySqlParameter("id", merchantid);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 修改封面
        /// </summary>
        /// <param name="cover"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Updatecover(string cover, string merchantid)
        {
            string sql = "update ms_merchant set cover=@cover where id=@id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("cover", cover);
            values[1] = new MySqlParameter("id", merchantid);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 更新营业时间
        /// </summary>
        /// <param name="business_hours"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Updatebusiness_hours(string business_hours, string merchantid)
        {
            string sql = "update ms_merchant set business_hours=@business_hours where id=@merchantid";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("business_hours", business_hours);
            values[1] = new MySqlParameter("merchantid", merchantid);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 更新营业状态
        /// </summary>
        /// <param name="status"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Updatestatus(string status, string merchantid)
        {
            string sql = "update ms_merchant set status=@status where id=@merchantid";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("status", status);
            values[1] = new MySqlParameter("merchantid", merchantid);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 商户登录
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public static ms_admins Login(string account, string pwd)
        {
            string sql = "select * from ms_admins where username=@username and password=@password";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("username", account);
            values[1] = new MySqlParameter("password", pwd);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            ms_admins admins = new ms_admins();
            if (rd.Read())
            {
                admins.id = rd["id"].ToString();
                admins.nickname = rd["nickname"].ToString();
                admins.username = rd["username"].ToString();
                admins.merchant_id = rd["merchant_id"].ToString();
                admins.login_ip = rd["login_ip"].ToString();
                admins.login_time = rd["login_time"].ToString();
                admins.register_time = rd["register_time"].ToString();
                admins.status = rd["status"].ToString();
                admins.admintype = rd["admintype"].ToString();
            }
            rd.Close();
            return admins;
        }
        /// <summary>
        /// 更新商家客服电话
        /// </summary>
        /// <param name="tel"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Updatetel(string tel, string merchantid)
        {
            string sql = "update ms_merchant set merchant_tel=@merchant_tel where id=@id";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("merchant_tel", tel);
            values[1] = new MySqlParameter("id", merchantid);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 查商户客服热线
        /// </summary>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static string Querytel(string merchantid)
        {
            string sql = "select merchant_tel from ms_merchant where id=@id";
            MySqlParameter[] values = new MySqlParameter[1];
            values[0] = new MySqlParameter("id", merchantid);
            MySqlDataReader rd = DBHelper.ExcutQuery(sql, values);
            string merchant_tel = "";
            if (rd.Read())
            {
                merchant_tel = rd["merchant_tel"].ToString();
            }
            rd.Close();
            return merchant_tel;
        }
        /// <summary>
        /// 更新地址
        /// </summary>
        /// <param name="province"></param>
        /// <param name="city"></param>
        /// <param name="area"></param>
        /// <param name="address"></param>
        /// <param name="longitude"></param>
        /// <param name="latitude"></param>
        /// <param name="merchantid"></param>
        /// <returns></returns>
        public static int Updateaddr(string province, string city, string area, string address, string longitude, string latitude, string merchantid)
        {
            string sql = "update ms_merchant set province=@province,city=@city,area=@area,address=@address,longitude=@longitude,latitude=@latitude where id=@id";
            MySqlParameter[] values = new MySqlParameter[7];
            values[0] = new MySqlParameter("id", merchantid);
            values[1] = new MySqlParameter("province", province);
            values[2] = new MySqlParameter("city", city);
            values[3] = new MySqlParameter("area", area);
            values[4] = new MySqlParameter("address", address);
            values[5] = new MySqlParameter("longitude", longitude);
            values[6] = new MySqlParameter("latitude", latitude);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 更新基本信息
        /// </summary>
        /// <param name="merchant"></param>
        /// <returns></returns>
        public static int Updateinfo(ms_merchant merchant)
        {
            string sql = "update ms_merchant set merchant_name=@merchant_name,logo=@logo,cover=@cover,advertising_map=@advertising_map,introduce=@introduce,delivery_price=@delivery_price,distribution_costs=@distribution_costs where id=@id";
            MySqlParameter[] values = new MySqlParameter[8];
            values[0] = new MySqlParameter("id", merchant.id);
            values[1] = new MySqlParameter("merchant_name", merchant.merchant_name);
            values[2] = new MySqlParameter("logo", merchant.logo);
            values[3] = new MySqlParameter("cover", merchant.cover);
            values[4] = new MySqlParameter("advertising_map", merchant.advertising_map);
            values[5] = new MySqlParameter("introduce", merchant.introduce);
            values[6] = new MySqlParameter("delivery_price", merchant.delivery_price);
            values[7] = new MySqlParameter("distribution_costs", merchant.distribution_costs);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 修改支付宝收款码
        /// </summary>
        /// <param name="code"></param>
        /// <param name="mid"></param>
        /// <returns></returns>
        public static int Updatealpaycode(string code, string mid)
        {
            string sql = "update ms_merchant set alipay_code=@alipay_code where id=@id";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("id", mid);
            values[1] = new MySqlParameter("alipay_code", code);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 修改微信收款码
        /// </summary>
        /// <param name="code"></param>
        /// <param name="mid"></param>
        /// <returns></returns>
        public static int Updatewechatcode(string code, string mid)
        {
            string sql = "update ms_merchant set wechat_code=@wechat_code where id=@id";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("id", mid);
            values[1] = new MySqlParameter("wechat_code", code);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 营业或打烊
        /// </summary>
        /// <param name="turnoff"></param>
        /// <param name="mid"></param>
        /// <returns></returns>
        public static int Updateturnoff(string turnoff, string mid)
        {
            string sql = "update ms_merchant set turnoff=@turnoff where id=@id";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("id", mid);
            values[1] = new MySqlParameter("turnoff", turnoff);
            return DBHelper.ExcutUpdate(sql, values);
        }
        /// <summary>
        /// 修改全天点餐时段状态
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static int Updateordering_today(string mid, string status)
        {
            string sql = "update ms_merchant set ordering_today=@ordering_today where id=@id";
            MySqlParameter[] values = new MySqlParameter[2];
            values[0] = new MySqlParameter("id", mid);
            values[1] = new MySqlParameter("ordering_today", status);
            return DBHelper.ExcutUpdate(sql, values);
        }
    }
}
